package com.wh.Ui;

import com.sun.jdi.event.StepEvent;
import com.wh.Domian.User;
import com.wh.Util.PopUpUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

//注册界面
public class RegisterJframe extends JFrame implements MouseListener {

    //输入框
    JTextField registerIdText = new JTextField();
    JTextField registerPasswordText = new JPasswordField();
    JTextField repetitionPasswordText = new JPasswordField();

    //注册按钮/重置按钮
    JButton register = new JButton();
    JButton reset = new JButton();

    JLabel returnLogin = new JLabel("返 回 登 录");


    public RegisterJframe(){
        //初始化主界面
        initJFrame();

        //页面内容
        initInterfaceContent();

        //显示页面
        this.setVisible(true);
    }

    //主页面
    public void initJFrame(){
        this.setSize(488,430);
        this.setTitle("拼图 注册");
        this.setAlwaysOnTop(true);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    //界面内容
    public void initInterfaceContent() {

        //注册id
        JLabel registerIdJlabel = new JLabel(new ImageIcon("image/register/注册用户名.png"));
        //加载注册id
        registerIdJlabel.setBounds(100, 120, 79, 17);
        this.getContentPane().add(registerIdJlabel);

        //加载id输入框
        registerIdText.setBounds(200, 124, 200, 30);
        this.getContentPane().add(registerIdText);


        //注册密码
        JLabel registerPasswordJlabel = new JLabel(new ImageIcon("image/register/注册密码.png"));
        //加载注册密码
        registerPasswordJlabel.setBounds(100,170,64,16);
        this.getContentPane().add(registerPasswordJlabel);
        //加载密码输入框
        registerPasswordText.setBounds(200,174,200,30);
        this.getContentPane().add(registerPasswordText);


        //再次输入密码
        JLabel repetitionPasswordJlabel = new JLabel(new ImageIcon("image/register/再次输入密码.png"));
        //加载再次输入密码
        repetitionPasswordJlabel.setBounds(100,220,97,17);
        this.getContentPane().add(repetitionPasswordJlabel);
        //加载再次输入密码框
        repetitionPasswordText.setBounds(200,224,200,30);
        this.getContentPane().add(repetitionPasswordText);


        //加载注册和重置按钮
        SwitchColors("register",true);
        register.setBounds(120, 280, 90, 40);
        register.setBorderPainted(false);  //去除按钮边框
        register.setContentAreaFilled(false);  //去除按钮背景
        //给按钮添加鼠标鼠标事件
        register.addMouseListener(this);
        this.getContentPane().add(register);

        SwitchColors("reset",true);
        reset.setBounds(280, 280, 90, 40);
        reset.setBorderPainted(false);
        reset.setContentAreaFilled(false);
        reset.addMouseListener(this);
        this.getContentPane().add(reset);

        //加载返回登录
        returnLogin.setBounds(210,330,90,40);
        returnLogin.addMouseListener(this);
        returnLogin.setForeground(new Color(0xA5E6E));
        returnLogin.setFont(returnLogin.getFont().deriveFont(16f));
        this.getContentPane().add(returnLogin);

        //加载背景图片
        JLabel background = new JLabel(new ImageIcon("image/register/background.png"));
        //设置大小和位置
        background.setBounds(0,0,470,390);
        //把背景图片加载到界面中
        this.getContentPane().add(background);
    }

    //登录和注册按钮变色
    public void SwitchColors(String str,boolean choose){

        switch (str){
            case "register" : {
                if (choose == true){
                    register.setIcon(new ImageIcon("image/register/注册按钮.png"));
                }else {
                    register.setIcon(new ImageIcon("image/register/注册按下.png"));
                }
                break;
            }
            case "reset" : {
                if (choose == true){
                    reset.setIcon(new ImageIcon("image/register/重置按钮.png"));
                }else {
                    reset.setIcon(new ImageIcon("image/register/重置按下.png"));
                }
                break;
            }
        }

    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Object obj = e.getSource();
        if (obj == returnLogin){
            // 隐藏当前页面
            this.setVisible(false);
            // 显示已存在的登录页面
            new LoginJframe();

        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object obj = e.getSource();
        //获取三个输入框的内容
        String id =  registerIdText.getText();
        String password =registerPasswordText.getText();
        String againPassword = repetitionPasswordText.getText();


        if (obj == register) {
            if (id.length() != 0 && password.length() != 0 && againPassword.length() != 0){
                //判断id是否存在
                if (LoginJframe.idIndex(id) < 0) {
                    if (password.equals(againPassword)){
                        //创建对象
                        LoginJframe.allUsers.add(new User(id,password));
                        registerIdText.setText("");
                        registerPasswordText.setText("");
                        repetitionPasswordText.setText("");
                        PopUpUtils.popWindows(PopUpUtils.ErrorTyoe.SUCCESSFULLY);
                    }else{
                        PopUpUtils.popWindows(PopUpUtils.ErrorTyoe.DISAFFINITY_ERROR);
                        registerPasswordText.setText("");
                        repetitionPasswordText.setText("");
                    }

                } else {
                    PopUpUtils.popWindows(PopUpUtils.ErrorTyoe.EXIST_ERROR);
                }
            }else {
                PopUpUtils.popWindows(PopUpUtils.ErrorTyoe.EMPTY_EEOR);
            }
        }else if(obj == reset){
            registerIdText.setText("");
            registerPasswordText.setText("");
            repetitionPasswordText.setText("");
        }

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        Object obj = e.getSource();
        if (obj == register){
            SwitchColors("register",false);
        } else if (obj == reset) {
            SwitchColors("reset",false);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        Object obj = e.getSource();
        if (obj == register){
            SwitchColors("register",true);
        } else if (obj == reset) {
            SwitchColors("reset",true);
        }
    }
}
